package com.zy.qudadid.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ZoomButtonsController;

//import com.zhy.http.okhttp.OkHttpUtils;
//import com.zhy.http.okhttp.callback.StringCallback;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import com.zy.lcpassenger.R;
import com.zy.qudadid.network.Const;
import com.zy.qudadid.presenter.base.BasePresenterImp;
import com.zy.qudadid.ui.activity.base.ToolBarActivity;
import com.zy.qudadid.utils.AESCrypt;
import com.zy.qudadid.utils.StringUtil;
import com.zy.util.JsonUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import okhttp3.Call;
import okhttp3.Request;

public class IntroduceEtcActivity extends ToolBarActivity {


    @Bind(R.id.txtContent)
    WebView txtContent;

    private String tag;

    @Override
    protected CharSequence provideTitle() {
        return "用户指南";
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_introduce_etc;
    }

    @Override
    public void initListeners() {

    }

    @Override
    protected void initThings(Bundle savedInstanceState) {
        super.initThings(savedInstanceState);
        tag = getIntent().getStringExtra("tag");

        if(tag.equals("4")){
            tvTitle.setText("用户指南");
        }else if(tag.equals("3")){
            tvTitle.setText("关于我们");
        }else if(tag.equals("2")){
            tvTitle.setText("法律条款");
        }else if(tag.equals("1")){
            tvTitle.setText("客服");
        }

        Map<String,String> map = new HashMap<>();
        map.put("type",tag);
        String json = JsonUtils.toJson(map);
        String data1 = null;
        try {
            data1 = AESCrypt.getInstance().encrypt(json);
            String d = StringUtil.replaceBlank(data1);
            OkHttpUtils.post()
                    .url(Const.BASE_URL+"api.php/User/settings")
                    .addParams("args",d)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int i) {

                        }

                        @Override
                        public void onResponse(String response,int i) {
                            JSONObject json;
                            try {
                                json = new JSONObject(response);
                                int code = json.getInt("code");
                                if(code == 200){
                                    String data = json.getString("datas");

                                    txtContent.loadUrl(data);
                                    txtContent.getSettings().setSupportZoom(true);
                                    // 设置出现缩放工具
                                    txtContent.getSettings().setBuiltInZoomControls(true);
                                    //自适应屏幕
                                    txtContent.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
                                    txtContent.getSettings().setUseWideViewPort(true);
                                    txtContent.getSettings().setLoadWithOverviewMode(true);
                                    txtContent.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
                                    txtContent.setBackgroundColor(0); // 设置背景色

                                    WebSettings webSettings =txtContent.getSettings();
                                    webSettings.setDisplayZoomControls(false);
                                    webSettings.setJavaScriptEnabled(true);
                                    webSettings.setBlockNetworkImage(false);
                                    setZoomControlGone(txtContent);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    //实现网页放大缩小控件隐藏
    public void setZoomControlGone(View view) {
        Class classType;
        Field field;
        try {
            classType = WebView.class;
            field = classType.getDeclaredField("mZoomButtonsController");
            field.setAccessible(true);
            ZoomButtonsController mZoomButtonsController = new ZoomButtonsController(view);
            mZoomButtonsController.getZoomControls().setVisibility(View.GONE);
            try {
                field.set(view, mZoomButtonsController);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }

    @Override
    public BasePresenterImp createPresenter() {
        return null;
    }


}
